package com.epalmpay.service.device.impl;

import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.terminalmanage.*;
import com.epalmpay.entity.*;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.enumdef.CommonEnum;
import com.epalmpay.mapper.*;
import com.epalmpay.service.device.IDeviceService;
import com.epalmpay.service.management.IMenuService;
import com.epalmpay.util.PropertyCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service("deviceService")
public class DeviceServiceImpl implements IDeviceService {

    //@Resource
    //private StatisticsDeviceMapper statisticsDeviceMapper;
    @Resource
    private StatisGroupMapper statisGroupMapper;

    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    private DevicePosMapper devicePosMapper;

    @Resource
    private DeviceScanMapper deviceScanMapper;

    @Resource
    private DeviceDeskCardMapper deviceDeskCardMapper;

    @Resource
    private DeviceScanDetailMapper deviceScanDetailMapper;

    @Resource
    private DeviceDeskCardDetailMapper deviceDeskCardDetailMapper;

    @Resource
    private DevicePosRepairMapper devicePosRepairMapper;

    @Resource
    private ShopGroupRelationMapper shopGroupRelationMapper;

    @Resource
    private AgentMapper agentMapper;

    @Resource
    private SysMessageBaseMapper sysMessageBaseMapper;

    @Resource
    private SysMessageInfoMapper sysMessageInfoMapper;

    @Resource
    private UserbaseMapper userbaseMapper;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private GroupMapper groupMapper;

    @Autowired
    private IMenuService menuService;

    /**
     * 登录判断
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<DeviceOutDTO> getStatisticsDeviceList(Long groupId, Integer industryID, Date joinStart, Date joinEnd, String name, Page page) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("industryID", industryID);
        param.put("joinStart", joinStart);
        param.put("joinEnd", joinEnd);
        param.put("name", name);
        param.put("groupId", groupId);

        int sumCount = statisGroupMapper.selectStatisticsDeviceListCount(param);
        page.setTotalResult(sumCount);
        if (sumCount <= 0) {
            return null;
        }
        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount() * (page.getCurrentPage() - 1));

        List<Map<String, Object>> statisticsDeviceList = statisGroupMapper.selectStatisticsDeviceList(param);

        List<DeviceOutDTO> result = new ArrayList<DeviceOutDTO>();

        for (Map<String, Object> statisticsDevice : statisticsDeviceList) {
            DeviceOutDTO deviceOutDTO = new DeviceOutDTO();

            deviceOutDTO.setGroupId((Long) (statisticsDevice.get("group_id")));
            deviceOutDTO.setMerchantName((String) statisticsDevice.get("merchant_name"));
            deviceOutDTO.setIndustryID((Integer) statisticsDevice.get("industry"));
            deviceOutDTO.setIndustryName((String) statisticsDevice.get("industry_name"));
            deviceOutDTO.setJoinDate((Date) (statisticsDevice.get("gmt_create")));
            deviceOutDTO.setMerchantNum((Integer) statisticsDevice.get("merchant_num"));
            deviceOutDTO.setPosNum((Integer) statisticsDevice.get("pos_num"));
            deviceOutDTO.setDeviceNum((Integer) statisticsDevice.get("device_num"));
            deviceOutDTO.setScanNum((Integer) statisticsDevice.get("scan_num"));
            deviceOutDTO.setDeskCardNum((Integer) statisticsDevice.get("deskcard_num"));
            deviceOutDTO.setTradeSumAmount((Long) statisticsDevice.get("trade_sum_amount"));
            deviceOutDTO.setIncomeSumAmount((Long) statisticsDevice.get("income_sum_amount"));

            result.add(deviceOutDTO);
        }

        return result;
    }


    /**
     * 插入数据表
     *
     * @param addInDTO
     * @return
     */
    @Override
    public int addDeviceInfo(DeviceAddInDTO addInDTO) {
        Device device = new Device();

        //根据shopId查询是否有代理商,有的话就插入到数据表中
        if (addInDTO.getShopId() != null) {
            Map<String, Object> result = shopGroupRelationMapper.selectByShopNameAndId(addInDTO.getGroupId(), addInDTO.getShopId());
            if (result != null) {
                device.setShopId((Long) result.get("shop_id"));
                device.setAgentId((Long) result.get("agent_id"));
            }
        }
        device.setGroupId(addInDTO.getGroupId());
        device.setPrintPic(addInDTO.getPrintPic());
        device.setCashierModeDeskcard(addInDTO.getCashierModeDeskcard());
        device.setCashierModePos(addInDTO.getCashierModePos());
        device.setCashierModeScan(addInDTO.getCashierModeScan());
        device.setDeviceName(addInDTO.getDeviceName());
        device.setPrintContent(addInDTO.getPrintContent());
        device.setGmtCreate(addInDTO.getGmtCreate());
        device.setGmtModified(addInDTO.getGmtModified());
        //默认新增设备状态是停用状态
        device.setDeviceStatus(BizEnum.DeviceStatus.Stop.getType());
        deviceMapper.insertSelective(device);

        if (device.getCashierModeScan()) {
            DeviceScan deviceScan = new DeviceScan();
            deviceScan.setDeviceId(device.getId());
            deviceScan.setScanMerchantName(addInDTO.getScanMerchantName());
            deviceScan.setScanMerchantId(addInDTO.getScanMerchantId());
            deviceScan.setScanOrganization(addInDTO.getScanOrganization());
            deviceScan.setScanTerminalId(addInDTO.getScanTerminalId());
            deviceScan.setFeeRate(addInDTO.getFeeRate());
            deviceScan.setGmtCreate(addInDTO.getGmtCreate());
            deviceScan.setGmtModified(addInDTO.getGmtModified());
            deviceScanMapper.insertSelective(deviceScan);
            if (addInDTO.getDeviceScanDetailOutDTOList().size() > 0) {
                List<DeviceScanDetail> deviceScanDetailList = new ArrayList<DeviceScanDetail>();
                for (int i = 0; i < addInDTO.getDeviceScanDetailOutDTOList().size(); i++) {
                    DeviceScanDetail deviceScanDetail = new DeviceScanDetail();
                    deviceScanDetail.setScanId(deviceScan.getId());
                    deviceScanDetail.setScanTerminalId(addInDTO.getDeviceScanDetailOutDTOList().get(i).getScanTerminalId());
                    deviceScanDetail.setScanTerminalName(addInDTO.getDeviceScanDetailOutDTOList().get(i).getScanTerminalName());
                    deviceScanDetail.setGmtCreate(addInDTO.getGmtCreate());
                    deviceScanDetail.setGmtModified(addInDTO.getGmtModified());
                    deviceScanDetail.setScanStatus(BizEnum.ScanTerminalStatus.UnUsed.getType());
                    deviceScanDetailList.add(deviceScanDetail);
                }
                deviceScanDetailMapper.insertByBatch(deviceScanDetailList);
            }


        }

        if (device.getCashierModeDeskcard()) {
            DeviceDeskCard deviceDeskCard = new DeviceDeskCard();
            deviceDeskCard.setDeviceId(device.getId());
            deviceDeskCard.setDeskcardMerchantName(addInDTO.getDeskcardMerchantName());
            deviceDeskCard.setDeskcardMerchantCode(addInDTO.getDeskcardMerchantCode());
            deviceDeskCard.setDeskcardMemMerchantCode(addInDTO.getDeskcardMemMerchantCode());
            deviceDeskCard.setFeeRate(addInDTO.getDeskfeeRate());
            deviceDeskCard.setDeskcardOrganization(addInDTO.getDeskcardOrganization());
            deviceDeskCard.setDeskcardAddress(addInDTO.getDeskcardAddress());
            deviceDeskCard.setGmtCreate(addInDTO.getGmtCreate());
            deviceDeskCard.setGmtModified(addInDTO.getGmtModified());
            deviceDeskCardMapper.insertSelective(deviceDeskCard);

            if (addInDTO.getDeviceDeskCardDetailOutDTOList().size() > 0) {
                List<DeviceDeskCardDetail> deviceDeskCardDetails = new ArrayList<DeviceDeskCardDetail>();

                for (int i = 0; i < addInDTO.getDeviceDeskCardDetailOutDTOList().size(); i++) {
                    DeviceDeskCardDetail deviceDeskCardDetail = new DeviceDeskCardDetail();
                    deviceDeskCardDetail.setDeskcardId(deviceDeskCard.getId());
                    deviceDeskCardDetail.setDeskcardName(addInDTO.getDeviceDeskCardDetailOutDTOList().get(i).getDeskcardName());
                    deviceDeskCardDetail.setDeskcardCode(addInDTO.getDeviceDeskCardDetailOutDTOList().get(i).getDeskcardCode());
                    //组装二维码生成地址
                    String QrLink = addInDTO.getDeskcardAddress() + "?" + "deskcardId="+deviceDeskCard.getId() + "&" +
                            "deskcardCode="+addInDTO.getDeviceDeskCardDetailOutDTOList().get(i).getDeskcardCode();
                    deviceDeskCardDetail.setQrcodeLink(QrLink);
                    deviceDeskCardDetail.setQrcodePath(QrLink);
                    deviceDeskCardDetail.setIsValid(true);
                    deviceDeskCardDetail.setGmtCreate(addInDTO.getGmtCreate());
                    deviceDeskCardDetail.setGmtModified(addInDTO.getGmtModified());
                    deviceDeskCardDetails.add(deviceDeskCardDetail);
                }
                deviceDeskCardDetailMapper.insertByBatch(deviceDeskCardDetails);
            }
        }

        if (device.getCashierModePos()) {
            DevicePos devicePos = new DevicePos();
            devicePos.setDeviceId(device.getId());
            devicePos.setPosTerminalSn(addInDTO.getPosTerminalSn());
            devicePos.setPosOrganization(addInDTO.getPosOrganization());
            devicePos.setPosMerchantId(addInDTO.getPosMerchantId());
            devicePos.setShopTerminalSn(addInDTO.getShopTerminalSn());
            devicePos.setAuthorizedCode(addInDTO.getAuthorizedCode());
            devicePos.setNetFeeRate(addInDTO.getNetFeeRate());
            devicePos.setNetMerchantName(addInDTO.getNetMerchantName());
            devicePos.setGmtCreate(addInDTO.getGmtCreate());
            devicePos.setGmtModified(addInDTO.getGmtModified());
            devicePos.setPosStatus((byte) BizEnum.PosStatus.Stop.getType());
            devicePosMapper.insertSelective(devicePos);
        }

        return 1;
    }


    /**
     * 返回新的POS信息
     *
     * @param pos_info
     * @return
     */
    @Override
    public DevicePos addDevicePosInfo(DeviceAddInDTO pos_info) {

        //更新原POS状态
        DevicePos devicePos = new DevicePos();
        devicePos.setId(pos_info.getPosId());
        devicePos.setPosStatus((byte) BizEnum.PosStatus.Abandoned.getType());
        devicePos.setGmtModified(new Date());
        devicePosMapper.updateByPrimaryKeySelective(devicePos);


        DevicePos pos = new DevicePos();
        pos.setDeviceId(pos_info.getId());
        pos.setPosTerminalSn(pos_info.getPosTerminalSn());
        pos.setPosOrganization(pos_info.getPosOrganization());
        pos.setPosMerchantId(pos_info.getPosMerchantId());
        pos.setShopTerminalSn(pos_info.getShopTerminalSn());
        pos.setAuthorizedCode(pos_info.getAuthorizedCode());
        pos.setNetFeeRate(pos_info.getNetFeeRate());
        pos.setNetMerchantName(pos_info.getNetMerchantName());
        pos.setGmtCreate(new Date());
        pos.setGmtModified(new Date());
        pos.setReplacePosId(pos_info.getPosId());
        if (pos_info.getDeviceStatus() == BizEnum.DeviceStatus.Normal.getType()) {
            pos.setPosStatus((byte) BizEnum.PosStatus.Normal.getType());
        } else if (pos_info.getDeviceStatus() == BizEnum.DeviceStatus.Stop.getType()) {
            pos.setPosStatus((byte) BizEnum.PosStatus.Stop.getType());
        }

        devicePosMapper.insertSelective(pos);

        //更新POS维修表
        DevicePosRepair devicePosRepair = new DevicePosRepair();
        devicePosRepair.setId(pos_info.getDevicePosRepair().getId());
//        devicePosRepair.setPosId(pos.getId());
        devicePosRepair.setRepairStatus(BizEnum.PosRepairStatus.Finished.getType());
        devicePosRepair.setGmtModified(new Date());
        devicePosRepairMapper.updateByPrimaryKeySelective(devicePosRepair);

        return pos;
    }


    /**
     * 查询大商户下 商户和代理商相关的设备信息
     *
     * @param page
     * @return
     * @throws Exception
     */
    @Override
    public List<GroupPostOutDTO> getDeviceList(GroupPosInDTO groupPosInDTO, Page page) throws Exception {

        int count = 0;

        Device device = new Device();
        device.setGroupId(groupPosInDTO.getGroupId());
        device.setShopId(groupPosInDTO.getShopId());
        device.setAgentId(groupPosInDTO.getAgentId());
        device.setPage(page);
        device.setDeviceStatus(groupPosInDTO.getDeviceStatus());
        device.setMerchantName(groupPosInDTO.getMerchantName());
        device.setAgentId(groupPosInDTO.getAgentId());
        device.setDistributionType(groupPosInDTO.getDistributionId());

        List<GroupPostOutDTO> result = new ArrayList<GroupPostOutDTO>();
        List<Map<String, Object>> deviceList = deviceMapper.selectByGroupIdlistPage(device);

        for (Map<String, Object> deviceItem : deviceList) {
            GroupPostOutDTO groupPostOutDTO = new GroupPostOutDTO();

            //过滤已废弃的POS信息
//            if (deviceItem.get("pos_status") != null && BizEnum.PosStatus.Abandoned.getType() != (Integer) deviceItem.get("pos_status")) {

                groupPostOutDTO.setId((Long) deviceItem.get("id"));
                groupPostOutDTO.setPosId((Long) deviceItem.get("pos_id"));
                groupPostOutDTO.setPosTerminalSn((String) deviceItem.get("pos_terminal_sn"));
                groupPostOutDTO.setPosStatus((Integer) deviceItem.get("pos_status"));
                groupPostOutDTO.setScanMerchantId((String) deviceItem.get("scan_merchant_id"));
                groupPostOutDTO.setShopId((Long) deviceItem.get("shop_id"));
                groupPostOutDTO.setDeskcardMerchantCode((String) deviceItem.get("deskcard_merchant_code"));
                groupPostOutDTO.setDeskcardMemMerchantCode((String) deviceItem.get("deskcard_mem_merchant_code"));
                groupPostOutDTO.setDeviceStatus((Integer) deviceItem.get("device_status"));
                groupPostOutDTO.setAgentId((Long) deviceItem.get("agent_id"));
                groupPostOutDTO.setAgentName((String) deviceItem.get("agent_name"));
                groupPostOutDTO.setDeviceName((String) deviceItem.get("device_name"));
                groupPostOutDTO.setShopName((String) deviceItem.get("shop_name"));
                groupPostOutDTO.setDeskcardCount((Long) deviceItem.get("deskcard_count"));
//                if (deviceItem.get("pos_id") != null){
//                    if (BizEnum.PosStatus.Abandoned.getType() != (Integer) deviceItem.get("pos_status")){
//                        result.add(groupPostOutDTO);
//                    }else{
//                        count++;
//                    }
//
//                }else {
//                    result.add(groupPostOutDTO);
//                }

                result.add(groupPostOutDTO);

//            }
        }


//        if (deviceList.size() - count > 0) {
//            page.setTotalResult(deviceList.size() - count);
//        }else{
//            page.setTotalResult(0);
//        }


        return result;
    }


    /**
     * 更新设备数据
     *
     * @param groupPosInDTO
     * @return
     */
    @Override
    public int updateDevice(GroupPosInDTO groupPosInDTO) {
        Device device = new Device();
        device.setId(groupPosInDTO.getId());
        if (groupPosInDTO.getDeviceStatus() != null) {
            device.setDeviceStatus(groupPosInDTO.getDeviceStatus());
        }
        if (groupPosInDTO.getShopId() != null) {
            device.setShopId(groupPosInDTO.getShopId());
        }
        if (groupPosInDTO.getAgentId() != null) {
            device.setAgentId(groupPosInDTO.getAgentId());
        }
        device.setGmtModified(new Date());
        return deviceMapper.updateByPrimaryKeySelective(device);
    }


    /**
     * 更新设备数据
     *
     * @return
     */
    @Override
    public int updateDeviceInfo(Device device) {

        //查询商户对应的代理商ID
        Map<String, Object> result = shopGroupRelationMapper.selectByShopNameAndId(device.getGroupId(), device.getShopId());
        if (result != null) {
//            device.setShopId((Long) result.get("shop_id"));
            device.setAgentId((Long) result.get("agent_id"));
        }
        return deviceMapper.updateByPrimaryKeySelective(device);
    }

    /**
     * 更新设备数据
     *
     * @return
     */
    @Override
    public int updateDevicePosInfo(DevicePos devicePos) {
        int result = 0;

        //先查询是否存在DeviceId,存在就更新，不存在插入

        DevicePos pos = devicePosMapper.selectByDeviceId(devicePos.getDeviceId());

        if (pos == null){
            devicePos.setGmtCreate(new Date());
            devicePos.setGmtModified(new Date());
            devicePos.setPosStatus((byte) BizEnum.PosStatus.Stop.getType());
            result = devicePosMapper.insertSelective(devicePos);
        }else{
            result = devicePosMapper.updateByDeviceId(devicePos);
        }

        return result;
    }

    @Override
    public int updateDevicePosById(DevicePos devicePos) {
        return devicePosMapper.updateByPrimaryKeySelective(devicePos);
    }

    /**
     * 编辑扫码数据
     *
     * @param deviceSm
     * @return
     */
    @Override
    public int updateDeviceSmInfo(DeviceAddInDTO deviceSm) {
        int result = 0;

        //先查询是否有这个设备号对应的扫码ID， 有就更新，没有就插入
        DeviceScan OlddeviceScan = deviceScanMapper.selectByDeviceId(deviceSm.getId());

        DeviceScan deviceScan = new DeviceScan();
        deviceScan.setDeviceId(deviceSm.getId());
        deviceScan.setScanMerchantName(deviceSm.getScanMerchantName());
        deviceScan.setScanMerchantId(deviceSm.getScanMerchantId());
        deviceScan.setScanOrganization(deviceSm.getScanOrganization());
        deviceScan.setScanTerminalId(deviceSm.getScanTerminalId());
        deviceScan.setFeeRate(deviceSm.getFeeRate());
        deviceScan.setGmtModified(new Date());
        if (OlddeviceScan != null) {
            result = deviceScanMapper.updateSmByDeviceId(deviceScan);
        }else{
            result = deviceScanMapper.insertSelective(deviceScan);
        }

        for (int i = 0; i < deviceSm.getDeviceScanDetailOutDTOList().size(); i++) {
            DeviceScanDetail deviceScanDetail = new DeviceScanDetail();
            deviceScanDetail.setId(deviceSm.getDeviceScanDetailOutDTOList().get(i).getId());
            if (OlddeviceScan == null) {
                deviceScanDetail.setScanId(deviceScan.getId());
            }else {
                deviceScanDetail.setScanId(deviceSm.getScanId());
            }
            deviceScanDetail.setScanTerminalId(deviceSm.getDeviceScanDetailOutDTOList().get(i).getScanTerminalId());
            deviceScanDetail.setScanTerminalName(deviceSm.getDeviceScanDetailOutDTOList().get(i).getScanTerminalName());
            deviceScanDetail.setGmtModified(new Date());
            if (deviceScanDetail.getId() != null) {
                result = deviceScanDetailMapper.updateByPrimaryKeySelective(deviceScanDetail);
            } else {
                deviceScanDetail.setGmtCreate(new Date());
                deviceScanDetail.setScanStatus(BizEnum.ScanTerminalStatus.UnUsed.getType());
                result = deviceScanDetailMapper.insertSelective(deviceScanDetail);
            }
        }

        return result;
    }


    /**
     * 编辑台卡数据
     *
     * @param deviceDesk
     * @return
     */
    @Override
    public int updateDeviceDeskInfo(DeviceAddInDTO deviceDesk) {
        int result = 0;

        //先查询是否有这个设备号对应的扫码ID， 有就更新，没有就插入
        DeviceDeskCard OlddeviceDeskCard = deviceDeskCardMapper.selectByDeviceId(deviceDesk.getId());

        DeviceDeskCard deviceDeskCard = new DeviceDeskCard();
        deviceDeskCard.setDeviceId(deviceDesk.getId());
        deviceDeskCard.setDeskcardMerchantName(deviceDesk.getDeskcardMerchantName());
        deviceDeskCard.setDeskcardMerchantCode(deviceDesk.getDeskcardMerchantCode());
        deviceDeskCard.setDeskcardMemMerchantCode(deviceDesk.getDeskcardMemMerchantCode());
        deviceDeskCard.setDeskcardOrganization(deviceDesk.getDeskcardOrganization());
        deviceDeskCard.setDeskcardAddress(deviceDesk.getDeskcardAddress());
        deviceDeskCard.setFeeRate(deviceDesk.getDeskfeeRate());
        deviceDeskCard.setGmtModified(new Date());

        if (OlddeviceDeskCard != null) {
            result = deviceDeskCardMapper.updateDeskByDeviceId(deviceDeskCard);
        }else{
            result = deviceDeskCardMapper.insertSelective(deviceDeskCard);
        }

        for (int i = 0; i < deviceDesk.getDeviceDeskCardDetailOutDTOList().size(); i++) {
            DeviceDeskCardDetail deviceDeskCardDetail = new DeviceDeskCardDetail();
            deviceDeskCardDetail.setId(deviceDesk.getDeviceDeskCardDetailOutDTOList().get(i).getId());
            if (OlddeviceDeskCard != null) {
                deviceDeskCardDetail.setDeskcardId(deviceDesk.getDeskId());
                deviceDeskCard.setId(OlddeviceDeskCard.getId());
            }else{
                deviceDeskCardDetail.setDeskcardId(deviceDeskCard.getId());
            }
            deviceDeskCardDetail.setDeskcardName(deviceDesk.getDeviceDeskCardDetailOutDTOList().get(i).getDeskcardName());
            deviceDeskCardDetail.setDeskcardCode(deviceDesk.getDeviceDeskCardDetailOutDTOList().get(i).getDeskcardCode());
            //组装二维码生成地址
            String QrLink = deviceDesk.getDeskcardAddress() + "?" + "deskcardId="+deviceDeskCard.getId() + "&" +
                    "deskcardCode="+deviceDesk.getDeviceDeskCardDetailOutDTOList().get(i).getDeskcardCode();
            deviceDeskCardDetail.setQrcodeLink(QrLink);
            deviceDeskCardDetail.setQrcodePath(QrLink);
            deviceDeskCardDetail.setIsValid(deviceDesk.getDeviceDeskCardDetailOutDTOList().get(i).getIsValid());
            deviceDeskCardDetail.setGmtModified(new Date());
            if (deviceDeskCardDetail.getId() != null) {
                result = deviceDeskCardDetailMapper.updateByPrimaryKeySelective(deviceDeskCardDetail);
            } else {
                deviceDeskCardDetail.setGmtCreate(new Date());
                deviceDeskCardDetail.setIsValid(true);
                result = deviceDeskCardDetailMapper.insertSelective(deviceDeskCardDetail);
            }
        }

        return result;
    }


    /**
     * 更新维修状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateDevicePosRepairStatus(Long posId, Long id, Integer status) {
        DevicePosRepair devicePosRepair = new DevicePosRepair();
        devicePosRepair.setId(id);
        devicePosRepair.setRepairStatus(status);
        devicePosRepair.setGmtModified(new Date());
        devicePosRepairMapper.updateByPrimaryKeySelective(devicePosRepair);

        DevicePos devicePos = new DevicePos();
        devicePos.setId(posId);
        devicePos.setPosStatus((byte) BizEnum.PosStatus.Stop.getType());
        devicePos.setGmtModified(new Date());
        devicePosMapper.updateByPrimaryKeySelective(devicePos);

        return 1;
    }

    /**
     * 删除台卡某条数据（这里是更改为更新状态为无效）
     *
     * @param id
     * @return
     */
    @Override
    public int delDeviceDeskDetailsById(Long id) {

//        return deviceDeskCardDetailMapper.deleteByPrimaryKey(id);
        DeviceDeskCardDetail deviceDeskCardDetail = new DeviceDeskCardDetail();
        deviceDeskCardDetail.setId(id);
        deviceDeskCardDetail.setIsValid(false);
        return deviceDeskCardDetailMapper.updateByPrimaryKeySelective(deviceDeskCardDetail);
    }

    /**
     * 删除扫码某条数据 这里是更改为更新状态为删除）
     *
     * @param id
     * @return
     */
    @Override
    public int delDeviceSmDetailsById(Long id) {
//        return deviceScanDetailMapper.deleteByPrimaryKey(id);

        DeviceScanDetail deviceScanDetail = new DeviceScanDetail();
        deviceScanDetail.setId(id);
        deviceScanDetail.setScanStatus(BizEnum.ScanTerminalStatus.Deleted.getType());
        return deviceScanDetailMapper.updateByPrimaryKeySelective(deviceScanDetail);
    }

    /**
     * 更新POS状态同时 将 设备状态为注销
     *
     * @param groupPosInDTO
     * @return
     */
    @Override
    public int updateDevicePosStatusAndDeviceStatus(GroupPosInDTO groupPosInDTO) {
        Device device = new Device();
        device.setId(groupPosInDTO.getId());
        device.setDeviceStatus(BizEnum.DeviceStatus.Close.getType());
        device.setGmtModified(new Date());
        deviceMapper.updateByPrimaryKeySelective(device);
        DevicePos devicePos = new DevicePos();
        devicePos.setDeviceId(groupPosInDTO.getId());
        devicePos.setPosStatus((byte) BizEnum.PosStatus.Abandoned.getType());
        devicePos.setGmtModified(new Date());
        devicePosMapper.updatePosStatusByDeviceId(devicePos);
        return 1;
    }

    @Override
    public int updateDevicePosByDeviceId(GroupPosInDTO groupPosInDTO) {


//        devicePos.setId(groupPosInDTO.getPosId());
        //通过设备ID 查询 不是废弃的POS状态的pos信息，再来更新
        Map<String, Object> devicePosInfo = devicePosMapper.getDevicePosInfoByDeviceId(groupPosInDTO.getId(), (byte) BizEnum.PosStatus.Abandoned.getType());
        if (devicePosInfo == null){
            return 1;
        }
        int posStatus = (int) devicePosInfo.get("pos_status");
        if (posStatus != BizEnum.PosStatus.Fault.getType()) {
            DevicePos devicePos = new DevicePos();
            devicePos.setId((Long) devicePosInfo.get("pos_id"));

            if (groupPosInDTO.getDeviceStatus() == BizEnum.DeviceStatus.Normal.getType()) {
                devicePos.setPosStatus((byte) BizEnum.PosStatus.Normal.getType());
            } else if (groupPosInDTO.getDeviceStatus() == BizEnum.DeviceStatus.Stop.getType()) {
                devicePos.setPosStatus((byte) BizEnum.PosStatus.Stop.getType());
            }


            devicePos.setGmtModified(new Date());

            return devicePosMapper.updateByPrimaryKeySelective(devicePos);
        }

        return 1;
    }

    /**
     * 解绑 将 shopId 和 angentId 设置为空
     *
     * @param groupPosInDTO
     * @return
     */
    @Override
    public int updateDeviceUnBind(GroupPosInDTO groupPosInDTO) {
        return deviceMapper.updateData(groupPosInDTO.getId(), groupPosInDTO.getUserOutDTO().getAgentId(), groupPosInDTO.getUserOutDTO().getShopId());
    }


    @Override
    public List<GroupPostOutDTO> getShopList(GroupPosInDTO groupPosInDTO) throws Exception {
        List<GroupPostOutDTO> groupPostOutDTOList = new ArrayList<GroupPostOutDTO>();
        List<Map<String, Object>> result = null;

        result = shopGroupRelationMapper.selectShopList(groupPosInDTO.getGroupId(), groupPosInDTO.getAgentId());
        if (result != null) {
            for (Map<String, Object> resultItem : result) {
                GroupPostOutDTO groupPostOutDTO = new GroupPostOutDTO();
                groupPostOutDTO.setShopId((Long) resultItem.get("shop_id"));
                groupPostOutDTO.setShopName((String) resultItem.get("merchant_name"));
                groupPostOutDTO.setAgentId((Long) resultItem.get("agent_id"));
                groupPostOutDTOList.add(groupPostOutDTO);
            }
        }
        return groupPostOutDTOList;
    }


    @Override
    public List<GroupPostOutDTO> getAgentIdAndName(Long groupId) throws Exception {
        List<GroupPostOutDTO> groupPostOutDTOList = new ArrayList<GroupPostOutDTO>();
        List<Agent> agentList = agentMapper.getAgentIdAndName(groupId);
        for (int i = 0; i < agentList.size(); i++) {
            GroupPostOutDTO groupPostOutDTO = new GroupPostOutDTO();
            groupPostOutDTO.setAgentId(agentList.get(i).getId());
            groupPostOutDTO.setAgentName(agentList.get(i).getMerchantName());
            groupPostOutDTOList.add(groupPostOutDTO);
        }
        return groupPostOutDTOList;
    }

    @Override
    public List<Agent> getAllAgent() throws Exception {
        return agentMapper.getAllAgent();
    }

    @Override
    public DeviceListOutDTO selectByPrimaryKey(DeviceInDTO deviceInDTO) {
        Device device = new Device();
        device.setAgentId(deviceInDTO.getAgentId());
        device.setGroupId(deviceInDTO.getGroupId());
        device.setMerchantName(deviceInDTO.getName());
        DeviceListOutDTO deviceListOutDTO = new DeviceListOutDTO();
        ArrayList<DeviceOutDTO> deviceOutDTOList = new ArrayList<DeviceOutDTO>();
        List<Device> listDevice = deviceMapper.selectByPrimary(device);
        for (Device devices : listDevice) {
            DeviceOutDTO deviceOutDTO = new DeviceOutDTO();
            deviceOutDTO.setId(devices.getId());
            deviceOutDTO.setDeviceName(devices.getDeviceName());
            deviceOutDTOList.add(deviceOutDTO);
            deviceListOutDTO.setDeviceOutList(deviceOutDTOList);
        }
        return deviceListOutDTO;
    }


    @Override
    public DeviceInfoOutDTO
    getDeviceInfo(Long id) {
        DeviceInfoOutDTO deviceInfoOutDTO = new DeviceInfoOutDTO();

        //查询设备所属信息
        Map<String, Object> result = deviceMapper.getDeviceInfo(id);
        if (result != null) {
            deviceInfoOutDTO.setId(id);
            deviceInfoOutDTO.setGroupId((Long) result.get("group_id"));
            deviceInfoOutDTO.setAgentId((Long) result.get("agent_id"));
            deviceInfoOutDTO.setShopId((Long) result.get("shop_id"));
            deviceInfoOutDTO.setGroupName((String) result.get("group_name"));
            deviceInfoOutDTO.setAgentName((String) result.get("agent_name"));
            deviceInfoOutDTO.setShopName((String) result.get("shop_name"));
            deviceInfoOutDTO.setCashierModePos((Boolean) result.get("cashier_mode_pos"));
            deviceInfoOutDTO.setCashierModeScan((Boolean) result.get("cashier_mode_scan"));
            deviceInfoOutDTO.setCashierModeDeskcard((Boolean) result.get("cashier_mode_deskcard"));
            deviceInfoOutDTO.setDeviceName((String) result.get("device_name"));
            deviceInfoOutDTO.setPrintContent((String) result.get("print_content"));
            deviceInfoOutDTO.setPrintPic((String) result.get("print_pic"));
            deviceInfoOutDTO.setDeviceStatus((Integer) result.get("device_status"));
            deviceInfoOutDTO.setReplaceDeviceId((Integer) result.get("replace_device_id"));

        }

        //查询pos入网信息
        Map<String, Object> devicePosInfo = devicePosMapper.getDevicePosInfoByDeviceId(id, (byte) BizEnum.PosStatus.Abandoned.getType());
        if (devicePosInfo != null) {
            deviceInfoOutDTO.setPosId((Long) devicePosInfo.get("pos_id"));
            deviceInfoOutDTO.setPosTerminalSn((String) devicePosInfo.get("pos_terminal_sn"));
            deviceInfoOutDTO.setPosOrganization((Integer) devicePosInfo.get("pos_organization"));
            deviceInfoOutDTO.setPosMerchantId((String) devicePosInfo.get("pos_merchant_id"));
            deviceInfoOutDTO.setShopTerminalSn((String) devicePosInfo.get("shop_terminal_sn"));
            deviceInfoOutDTO.setAuthorizedCode((String) devicePosInfo.get("authorized_code"));
            deviceInfoOutDTO.setNetFeeRate((Integer) devicePosInfo.get("pos_fee_rate"));
            deviceInfoOutDTO.setNetMerchantName((String) devicePosInfo.get("net_merchant_name"));
            deviceInfoOutDTO.setPosStatus((Integer) devicePosInfo.get("pos_status"));
            deviceInfoOutDTO.setReplacePosId((Long) devicePosInfo.get("replace_pos_id"));

            //查询POS维修信息
            DevicePosRepair devicePosRepairInfo = devicePosRepairMapper.getDevicePosRepairInfoByPosIdToEnity((Long) devicePosInfo.get("pos_id"));
            if (devicePosRepairInfo != null) {
                deviceInfoOutDTO.setDevicePosRepair(devicePosRepairInfo);
            }
        }


        //查询台卡信息
        Map<String, Object> deviceDeskCardInfo = deviceDeskCardMapper.getDeviceDeskCardInfo(id);
        if (deviceDeskCardInfo != null) {
            deviceInfoOutDTO.setDeskCardId((Long) deviceDeskCardInfo.get("desk_card_id"));
            deviceInfoOutDTO.setDeskcardMerchantName((String) deviceDeskCardInfo.get("deskcard_merchant_name"));
            deviceInfoOutDTO.setDeskcardOrganization((Integer) deviceDeskCardInfo.get("deskcard_organization"));
            deviceInfoOutDTO.setDeskcardMerchantCode((String) deviceDeskCardInfo.get("deskcard_merchant_code"));
            deviceInfoOutDTO.setDeskcardMemMerchantCode((String) deviceDeskCardInfo.get("deskcard_mem_merchant_code"));
            deviceInfoOutDTO.setDeskfeeRate((Integer) deviceDeskCardInfo.get("desk_fee_rate"));
            deviceInfoOutDTO.setDeskcardAddress((String) deviceDeskCardInfo.get("deskcard_address"));

            //台卡详情
            List<Map<String, Object>> deskCardDetailInfoList = deviceDeskCardDetailMapper.getDeviceDeskCardDetailInfoByDeskCardId(deviceInfoOutDTO.getDeskCardId());
            if (deskCardDetailInfoList != null && deskCardDetailInfoList.size() > 0) {

                List<DeviceDeskCardDetail> deskCardDetails = new ArrayList<DeviceDeskCardDetail>();
                for (Map<String, Object> deskCardDetail : deskCardDetailInfoList) {
                    DeviceDeskCardDetail deviceDeskCardDetail = new DeviceDeskCardDetail();
                    deviceDeskCardDetail.setId((Long) deskCardDetail.get("id"));
                    deviceDeskCardDetail.setDeskcardCode((String) deskCardDetail.get("deskcard_code"));
                    deviceDeskCardDetail.setDeskcardName((String) deskCardDetail.get("deskcard_name"));
                    deviceDeskCardDetail.setQrcodeLink((String) deskCardDetail.get("qrcode_link"));
                    deviceDeskCardDetail.setQrcodePath((String) deskCardDetail.get("qrcode_path"));
                    deviceDeskCardDetail.setIsValid((Boolean) deskCardDetail.get("is_valid"));
                    deskCardDetails.add(deviceDeskCardDetail);
                }

                deviceInfoOutDTO.setDeviceDeskCardDetailOutDTOList(deskCardDetails);
            }
        }

        //查询扫码信息
        Map<String, Object> deviceScanInfo = deviceScanMapper.getDeviceScanInfo(id);
        if (deviceScanInfo != null) {
            deviceInfoOutDTO.setScanId((Long) deviceScanInfo.get("scan_id"));
            deviceInfoOutDTO.setScanMerchantName((String) deviceScanInfo.get("scan_merchant_name"));
            deviceInfoOutDTO.setScanOrganization((Integer) deviceScanInfo.get("scan_organization"));
            deviceInfoOutDTO.setScanMerchantId((String) deviceScanInfo.get("scan_merchant_id"));
            deviceInfoOutDTO.setScanFeeRate((Integer) deviceScanInfo.get("scan_fee_rate"));
            deviceInfoOutDTO.setScanTerminalId((String) deviceScanInfo.get("scan_terminal_id"));

            //扫码详情
            List<Map<String, Object>> deviceScanDetailInfoList = deviceScanDetailMapper.getDeviceScanDetailInfo(deviceInfoOutDTO.getScanId());


            if (deviceScanDetailInfoList != null && deviceScanDetailInfoList.size() > 0) {
                List<DeviceScanDetail> deviceScanDetails = new ArrayList<DeviceScanDetail>();
                for (Map<String, Object> scanDetail : deviceScanDetailInfoList) {
                    DeviceScanDetail deviceScanDetail = new DeviceScanDetail();
                    deviceScanDetail.setId((Long) scanDetail.get("id"));
                    deviceScanDetail.setScanTerminalName((String) scanDetail.get("scan_terminal_name"));
                    deviceScanDetail.setScanTerminalId((String) scanDetail.get("scan_terminal_id"));
                    deviceScanDetail.setScanStatus((Integer) scanDetail.get("scan_status"));
                    deviceScanDetails.add(deviceScanDetail);
                }
                deviceInfoOutDTO.setDeviceScanDetailOutDTOList(deviceScanDetails);
            }
        }


        return deviceInfoOutDTO;
    }


    @Override
    public DeviceDeskCardDetail getDeviceCardDetailById(Long id) {
        return deviceDeskCardDetailMapper.selectByPrimaryKey(id);
    }
    
    
    public  DeviceDeskCard getDeviceDeskCardId(Long id){
    	DeviceDeskCard deviceDeskCard = deviceDeskCardMapper.selectByPrimaryKey(id);
		return deviceDeskCard;
    }


    @Override
    public DeviceInfoOutDTO getDeviceInfoByReplacePosId(Long id) {

        DeviceInfoOutDTO deviceInfoOutDTO = new DeviceInfoOutDTO();

        Map<String, Object> result = devicePosMapper.getDeviceInfoByOldPosId(id);

        if (result != null) {
            deviceInfoOutDTO.setGroupName((String) result.get("groupName"));
            deviceInfoOutDTO.setAgentName((String) result.get("agentName"));
            deviceInfoOutDTO.setShopName((String) result.get("shopName"));
            deviceInfoOutDTO.setDeviceName((String) result.get("device_name"));
            deviceInfoOutDTO.setCashierModePos((Boolean) result.get("cashier_mode_pos"));
            deviceInfoOutDTO.setCashierModeScan((Boolean) result.get("cashier_mode_scan"));
            deviceInfoOutDTO.setCashierModeDeskcard((Boolean) result.get("cashier_mode_deskcard"));
            deviceInfoOutDTO.setPrintContent((String) result.get("print_content"));
            deviceInfoOutDTO.setPrintPic((String) result.get("print_pic"));


            deviceInfoOutDTO.setPosTerminalSn((String) result.get("pos_terminal_sn"));
            deviceInfoOutDTO.setPosOrganization((Integer) result.get("pos_organization"));
            deviceInfoOutDTO.setPosMerchantId((String) result.get("pos_merchant_id"));
            deviceInfoOutDTO.setShopTerminalSn((String) result.get("shop_terminal_sn"));
            deviceInfoOutDTO.setAuthorizedCode((String) result.get("authorized_code"));
            deviceInfoOutDTO.setNetFeeRate((Integer) result.get("net_fee_rate"));
            deviceInfoOutDTO.setNetMerchantName((String) result.get("net_merchant_name"));

        }


        return deviceInfoOutDTO;
    }

    @Override
    public int addRepairInfo(DevicePosRepairInDTO devicePosRepairInDTO) throws Exception {


        //插入维修表
        DevicePosRepair devicePosRepair = new DevicePosRepair();

        PropertyCopyUtil.copyProperties(devicePosRepairInDTO.getDevicePosRepair(), devicePosRepair);
        devicePosRepair.setGmtModified(new Date());
        devicePosRepair.setGmtCreate(new Date());
        devicePosRepair.setRepairStatus(BizEnum.PosRepairStatus.RepairApply.getType());
        devicePosRepairMapper.insertSelective(devicePosRepair);


        //更新POS状态为故障
        DevicePos devicePos = new DevicePos();
        devicePos.setId(devicePosRepairInDTO.getDevicePosRepair().getPosId());
        devicePos.setPosStatus((byte) BizEnum.PosStatus.Fault.getType());
        devicePosMapper.updateByPrimaryKeySelective(devicePos);


        //插入系统消息表
        SysMessageBase sysMessageBase = new SysMessageBase();
        sysMessageBase.setGmtCreate(new Date());
        sysMessageBase.setGmtModified(new Date());
        sysMessageBase.setPosRepairId(devicePosRepair.getId());
        sysMessageBase.setMessageType((byte) BizEnum.MessageType.POSRepair.getType());
        sysMessageBase.setMemo(devicePosRepair.getProblemDesc());
        sysMessageBase.setSendFrom(devicePosRepairInDTO.getSendFrom());
        sysMessageBase.setSendStatus((byte) 0);  //发送失败
        sysMessageBaseMapper.insertSelective(sysMessageBase);

        //查询掌富后台管理员和操作员ID 插入 消息详情表
        List<Userbase> ZfWorkerList = new ArrayList<>();
        ZfWorkerList.addAll(userbaseMapper.listUserIdByGroupIdAndUserType(null, CommonEnum.UserType.ZfWorker.getType()));

        if (ZfWorkerList.size() > 0) {
            List<SysMessageInfo> sysMessageInfoList = new ArrayList<>();

            for (int i = 0; i < ZfWorkerList.size(); i++) {
                SysMessageInfo sysMessageInfo = new SysMessageInfo();
                sysMessageInfo.setGmtCreate(new Date());
                sysMessageInfo.setGmtModified(new Date());
                sysMessageInfo.setHasRead((byte) 1);
                sysMessageInfo.setMessageId(sysMessageBase.getId());
                sysMessageInfo.setReceviceUserId(ZfWorkerList.get(i).getId());
                sysMessageInfoList.add(sysMessageInfo);
            }
            if (sysMessageInfoList.size() > 0) {
                sysMessageInfoMapper.insertByBatch(sysMessageInfoList);
            }
        }

        List<Userbase> ZfOperatorList = new ArrayList<>();
        ZfOperatorList.addAll(userbaseMapper.listUserIdByGroupIdAndUserType(null, CommonEnum.UserType.ZfOperator.getType()));

        //移除无权限读取系统消息的操作员
        if (ZfOperatorList.size() > 0) {
            for (int i = 0; i < ZfWorkerList.size(); i++) {
                long userId = ZfWorkerList.get(i).getId();
                if (!menuService.MenuRights(userId, 186)) {   //78表示系统消息，这里是从数据库直接拿来的
                    ZfWorkerList.remove(i);
                }
            }
            List<SysMessageInfo> sysMessageInfoList = new ArrayList<>();
            for (int i = 0; i < ZfOperatorList.size(); i++) {
                SysMessageInfo sysMessageInfo = new SysMessageInfo();
                sysMessageInfo.setGmtCreate(new Date());
                sysMessageInfo.setGmtModified(new Date());
                sysMessageInfo.setHasRead((byte) 1);
                sysMessageInfo.setMessageId(sysMessageBase.getId());
                sysMessageInfo.setReceviceUserId(ZfOperatorList.get(i).getId());
                sysMessageInfoList.add(sysMessageInfo);
            }
            if (sysMessageInfoList.size() > 0) {
                sysMessageInfoMapper.insertByBatch(sysMessageInfoList);
            }
        }


        //查询大商户下管理员和操作员 插入 消息详情表
        List<Userbase> BusinessManagerList = new ArrayList<>();
        BusinessManagerList.addAll(userbaseMapper.listUserIdByGroupIdAndUserType(devicePosRepairInDTO.getGroupId(), CommonEnum.UserType.BusinessManager.getType()));
        if (BusinessManagerList.size() > 0) {

            List<SysMessageInfo> sysMessageInfoList = new ArrayList<>();
            for (int i = 0; i < BusinessManagerList.size(); i++) {
                SysMessageInfo sysMessageInfo = new SysMessageInfo();
                sysMessageInfo.setGmtCreate(new Date());
                sysMessageInfo.setGmtModified(new Date());
                sysMessageInfo.setHasRead((byte) 1);
                sysMessageInfo.setMessageId(sysMessageBase.getId());
                sysMessageInfo.setReceviceUserId(BusinessManagerList.get(i).getId());
                sysMessageInfoList.add(sysMessageInfo);
            }
            if (sysMessageInfoList.size() > 0) {
                sysMessageInfoMapper.insertByBatch(sysMessageInfoList);
            }
        }
        List<Userbase> BusinessOperatorList = new ArrayList<>();
        BusinessManagerList.addAll(userbaseMapper.listUserIdByGroupIdAndUserType(devicePosRepairInDTO.getGroupId(), CommonEnum.UserType.BusinessOperator.getType()));
        if (BusinessOperatorList.size() > 0) {

            for (int i = 0; i < BusinessManagerList.size(); i++) {
                long userId = BusinessManagerList.get(i).getId();
                if (!menuService.MenuRights(userId, 211)) {   //78表示系统消息，这里是从数据库直接拿来的
                    BusinessManagerList.remove(i);
                }
            }

            List<SysMessageInfo> sysMessageInfoList = new ArrayList<>();
            for (int i = 0; i < BusinessOperatorList.size(); i++) {
                SysMessageInfo sysMessageInfo = new SysMessageInfo();
                sysMessageInfo.setGmtCreate(new Date());
                sysMessageInfo.setGmtModified(new Date());
                sysMessageInfo.setHasRead((byte) 1);
                sysMessageInfo.setMessageId(sysMessageBase.getId());
                sysMessageInfo.setReceviceUserId(BusinessOperatorList.get(i).getId());
                sysMessageInfoList.add(sysMessageInfo);
            }
            if (sysMessageInfoList.size() > 0) {
                sysMessageInfoMapper.insertByBatch(sysMessageInfoList);
            }
        }

        if (devicePosRepairInDTO.getUserOutDTO().getAgentId() != null) {
            //查询代理商 插入 消息详情表
            List<Userbase> AgentsList = new ArrayList<>();
            AgentsList.addAll(userbaseMapper.listUserIdByGroupIdAndUserType(devicePosRepairInDTO.getGroupId(), CommonEnum.UserType.Agents.getType()));
            if (AgentsList.size() > 0) {
                List<SysMessageInfo> sysMessageInfoList = new ArrayList<>();
                for (int i = 0; i < AgentsList.size(); i++) {
                    SysMessageInfo sysMessageInfo = new SysMessageInfo();
                    sysMessageInfo.setGmtCreate(new Date());
                    sysMessageInfo.setGmtModified(new Date());
                    sysMessageInfo.setHasRead((byte) 1);
                    sysMessageInfo.setMessageId(sysMessageBase.getId());
                    sysMessageInfo.setReceviceUserId(AgentsList.get(i).getId());
                    sysMessageInfoList.add(sysMessageInfo);
                }
                if (sysMessageInfoList.size() > 0) {
                    sysMessageInfoMapper.insertByBatch(sysMessageInfoList);
                }
            }
        }

        if (devicePosRepairInDTO.getUserOutDTO().getShopId() != null) {

            //查询门店 插入 消息详情表
            List<Userbase> StoreList = new ArrayList<>();
            StoreList.addAll(userbaseMapper.listUserIdByGroupIdAndUserType(devicePosRepairInDTO.getGroupId(), CommonEnum.UserType.Store.getType()));
            if (StoreList.size() > 0) {
                List<SysMessageInfo> sysMessageInfoList = new ArrayList<>();
                for (int i = 0; i < StoreList.size(); i++) {
                    SysMessageInfo sysMessageInfo = new SysMessageInfo();
                    sysMessageInfo.setGmtCreate(new Date());
                    sysMessageInfo.setGmtModified(new Date());
                    sysMessageInfo.setHasRead((byte) 1);
                    sysMessageInfo.setMessageId(sysMessageBase.getId());
                    sysMessageInfo.setReceviceUserId(StoreList.get(i).getId());
                    sysMessageInfoList.add(sysMessageInfo);
                }
                if (sysMessageInfoList.size() > 0) {
                    sysMessageInfoMapper.insertByBatch(sysMessageInfoList);
                }
            }
        }

        sysMessageBase.setSendStatus((byte) 1);  //发送成功
        return sysMessageBaseMapper.updateByPrimaryKey(sysMessageBase);
    }


	

    @Override
    public DeviceOutDTO getDeviceGroupId(String deskcardCode, Long deskcardId) throws ApplicationException {
        DeviceOutDTO deviceOutDTO = new DeviceOutDTO();
        Device device = deviceMapper.selectByPrimaryCode(deskcardCode, deskcardId);
        if(device==null){
        	 deviceOutDTO.setErrorCode(Rescode.FAIL, "台卡不存在");
        	 return deviceOutDTO;
        }else if(device.getDeviceStatus().equals(BizEnum.DeviceStatus.Normal.getType())){
	        	 //已分配门店 显示门店名称 未分配 显示大商户名称
	            if (device.getShopId()!= null) {
	                Shop shop = shopMapper.selectByPrimaryKey(device.getShopId());
	                deviceOutDTO.setMerchantName(shop.getMerchantName());
	                deviceOutDTO.setShopId(shop.getId());
	            } else {
//	                Group group = groupMapper.selectByPrimaryKey(device.getGroupId());
//	                deviceOutDTO.setMerchantName(group.getMerchantName());
	            	deviceOutDTO.setErrorCode(Rescode.FAIL, "台卡未分配门店,暂不能使用");
	            }
		        deviceOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功");   
		        deviceOutDTO.setGroupId(device.getGroupId());
        }else {
        	deviceOutDTO.setErrorCode(Rescode.FAIL, "台卡"+BizEnum.DeviceStatus.fromType(device.getDeviceStatus()).getName()+"请联系系统管理员");
        }
        return deviceOutDTO;
    }


	@Override
	public DeviceListOutDTO getByDeviceShopId(Long shopId) throws ApplicationException{
		DeviceListOutDTO deviceListOutDTO=new DeviceListOutDTO();
		List<Device> deviceList=deviceMapper.selectByDeviceShopId(shopId);
		ArrayList<DeviceOutDTO> deviceOutList = new ArrayList<DeviceOutDTO>();
		for (Device device : deviceList) {
			DeviceOutDTO deviceOutDTO=new DeviceOutDTO();
			try {
				PropertyCopyUtil.copyPropertiesExclude(device, deviceOutDTO);
				deviceOutList.add(deviceOutDTO);
			} catch (Exception e) {
				e.printStackTrace();
				throw new ApplicationException("对象拷贝异常");
			}
		} 
		deviceListOutDTO.setDeviceOutList(deviceOutList);
		deviceListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
		return deviceListOutDTO;
	}
}
